package indi.zhifa.engine.core.excel.entity;



import indi.zhifa.engine.core.excel.entity.enums.EExcelErrorCode;
import indi.zhifa.engine.core.excel.entity.exception.ExcelException;
import lombok.Data;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;

import java.util.List;

@Data
public abstract class BaseExcelSheetRowData {
    public abstract void init();
    public abstract void addCell(List<Cell> pCellList, ExcelHeader pHeader);
    public abstract void finish();

    protected Double checkDouble(Cell pCell){
        if(pCell.getCellType() == CellType.NUMERIC){
            return pCell.getNumericCellValue();
        }else if(pCell.getCellType() == CellType.STRING){
            String strVal = pCell.getStringCellValue();
            try{

                return Double.parseDouble(strVal);
            }catch (Exception ex){
                throw new ExcelException(EExcelErrorCode.CELL_FORMAT_ERROR,strVal+" 无法转换为double");
            }
        }
        return null;
    }

    protected int checkYearVal(String pAYearVal){
        int idx = pAYearVal.indexOf("年");
        if(idx < 0){
            throw new ExcelException(EExcelErrorCode.CELL_FORMAT_ERROR,pAYearVal+" 格式不正确，正确格式是%d年");
        }
        String strYear = pAYearVal.substring(0,idx);
        try{
            return Integer.parseInt(strYear);
        }catch (Exception ex){
            throw new ExcelException(EExcelErrorCode.CELL_FORMAT_ERROR,pAYearVal+" 无法转换为%d年");
        }
    }
}
